package com.googlecode.mad.mvntools.washotdeployer;

import java.io.File;

/**
 * - Apache License 2.0 - <br>
 * Mojo to send local projects to websphere in expanded form <br>
 * <br>
 * LastChangedBy: $Author: marthaler.worb@gmail.com $<br>
 * LastChangedDate: $Date: 2010-12-29 14:23:06 +0100 (Wed, 29 Dec 2010) $
 * 
 * @author daniel marthaler
 * @version $Revision: 22 $
 * @since 03.08.2011
 * @goal sync
 */
public class Synchronize extends AbstractHotdeployer {

	public Synchronize() {
		super();
		this.fileName = "SettingsUtil:backup";
	}

	@Override
	@SuppressWarnings("unchecked")
	protected void myExecute() {

		File localFile = null;
		File remoteFile = null;
		String artifactLocal = null;
		final String packaging = super.mavenProject.getPackaging();

		final String artifact = super.mavenProject.getArtifact()
				.getArtifactId()
				+ "-"
				+ super.mavenProject.getVersion()
				+ "."
				+ super.mavenProject.getPackaging();
		this.getLog().info(artifact);

		if (packaging.equals("war")) {
			this.getLog().info("copy .war");

			artifactLocal = super.mavenProject.getBasedir() + "/" + "/target/"
					+ artifact.replace(".war", "");

			localFile = new File(artifactLocal);
			remoteFile = super.getFileForArtifact(artifact);
			
			// without version?
			if(remoteFile == null){
				String shortArt = artifact.replace("-"+super.mavenProject.getVersion(), "");
				this.getLog().info("--> " + shortArt);
				remoteFile = super.getFileForArtifact(shortArt);
			}

			if (remoteFile == null) {
				getLog().info(
						"In the EAR was no: "
								+ artifact
								+ " will skip the copy, maybe not the right ear is installed?");
				return;
			}
			super.copyFileOrFolder(localFile, remoteFile);
		}

		if (packaging.equals("jar")) {
			artifactLocal = super.mavenProject.getBasedir() + "/" + "/target/"
					+ artifact;
			this.getLog().info("copy .jar");
			localFile = new File(artifactLocal);
			remoteFile = super.getFileForArtifact(artifact);

			if (remoteFile == null) {
				getLog().info(
						"In the EAR was no: "
								+ artifact
								+ " will skip the copy, maybe not the right ear is installed?");
				return;
			}

			super.copyFileOrFolder(localFile, remoteFile);
		}

		if (packaging.equals("rar")) {
			this.getLog().info("copy .rar");
		}

	}
}
